package com.xkdx.guangguang;

import java.io.File;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.MotionEvent;
import android.view.View;

import com.xkdx.caipiao.R;

public class EditerUpload extends Activity {
	private static String TAG = "EditerUpload";
	public static Bitmap editeUploadBitmap = null;
	public static final int NONE = 0;
	public static final int PHOTOHRAPH = 1;// 拍照
	public static final int PHOTOZOOM = 2; // 缩放
	public static final int PHOTORESOULT = 3;// 结果
	public static final String IMAGE_UNSPECIFIED = "image/*";
	public static boolean isNative = false;
	public static Uri cutUri, editUri;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.upload_photo_choice);

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		finish();
		return true;
	}

	public void exitbutton1(View v) {

		getNativePhoto();
	}

	public void exitbutton0(View v) {

		getTakePhoto();

	}

	public void exitbutton(View v) {
		finish();
	}

	@Override
	public void onBackPressed() {
		finish();
	}

	// 获取本地图片
	public void getNativePhoto() {
		isNative = true;
		Intent intent = new Intent(Intent.ACTION_PICK, null);
		intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
		File file = new File(Environment.getExternalStorageDirectory() + "/GuangGuang/photo");
		if (!file.exists()) {
			file.mkdirs();
		}
		File cutFile = new File(Environment.getExternalStorageDirectory() + "/GuangGuang/photo" + "/editCut.jpg");
		cutUri = Uri.fromFile(cutFile);
		startActivityForResult(intent, PHOTOZOOM);

	}

	// 获取照相图片
	public void getTakePhoto() {
		isNative = false;
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		File file = new File(Environment.getExternalStorageDirectory() + "/GuangGuang/photo");
		if (!file.exists()) {
			file.mkdirs();
		}
		File file1 = new File(Environment.getExternalStorageDirectory() + "/GuangGuang/photo" + "/edit.jpg");
		editUri = Uri.fromFile(file1);
		File cutFile = new File(Environment.getExternalStorageDirectory() + "/GuangGuang/photo" + "/editCut.jpg");
		cutUri = Uri.fromFile(cutFile);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, editUri);
		startActivityForResult(intent, PHOTOHRAPH);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == NONE) {
			editeUploadBitmap = null;
			return;
		}

		if (requestCode == PHOTOHRAPH) {
			startEditPhotoZoom(editUri, cutUri);

		}
		if (requestCode == PHOTOZOOM) {
			editUri = data.getData();
			startEditPhotoZoom(editUri, cutUri);
		}

		if (requestCode == PHOTORESOULT) {
			if (cutUri != null) {
				editeUploadBitmap = decodeUriAsBitmap(cutUri);
				finish();
			}

		}

		super.onActivityResult(requestCode, resultCode, data);

	}

	public void startEditPhotoZoom(Uri starturi, Uri endUri) {
		Intent intent = new Intent("com.android.camera.action.CROP");

		intent.setDataAndType(starturi, IMAGE_UNSPECIFIED);
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", 27);
		intent.putExtra("aspectY", 10);
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX", 540);
		intent.putExtra("outputY", 200);
		intent.putExtra("scale", true);
		intent.putExtra("return-data", false);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, endUri);
		intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
		intent.putExtra("noFaceDetection", true);
		// Log.i(TAG, "kaishi jian qie");
		startActivityForResult(intent, PHOTORESOULT);
	}

	private Bitmap getimage(String srcPath) {
		BitmapFactory.Options newOpts = new BitmapFactory.Options();
		// 开始读入图片，此时把options.inJustDecodeBounds 设回true了
		newOpts.inJustDecodeBounds = true;
		Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此时返回bm为空

		newOpts.inJustDecodeBounds = false;
		int w = newOpts.outWidth;
		int h = newOpts.outHeight;
		// Log.i(TAG, "宽度：" + w);
		// 现在主流手机比较多是800*480分辨率，所以高和宽我们设置为
		float hh = 800f;// 这里设置高度为800f
		float ww = 391f;// 这里设置宽度为480f
		// 缩放比。由于是固定比例缩放，只用高或者宽其中一个数据进行计算即可
		int be = 1;// be=1表示不缩放
		if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放
			be = (int) (newOpts.outWidth / ww);
		} else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放
			be = (int) (newOpts.outHeight / hh);
		}
		if (be <= 0)
			be = 1;
		newOpts.inSampleSize = be;// 设置缩放比例
		// Log.i(TAG, "缩放比：" + be);
		// 重新读入图片，注意此时已经把options.inJustDecodeBounds 设回false了
		Bitmap newbitmap = BitmapFactory.decodeFile(srcPath, newOpts);
		// 压缩好比例大小后再进行质量压缩
		return newbitmap;
	}

	private Bitmap big(Bitmap b, float x, float y) {
		int w = b.getWidth();
		int h = b.getHeight();
		float sx = (float) x / w;// 要强制转换，不转换总是死掉。
		float sy = (float) y / h;
		Matrix matrix = new Matrix();
		matrix.postScale(sx, sy); // 长和宽放大缩小的比例
		Bitmap resizeBmp = Bitmap.createBitmap(b, 0, 0, w, h, matrix, true);
		return resizeBmp;
	}

	private Bitmap decodeUriAsBitmap(Uri uri) {

		Bitmap bitmap = null;

		try {
			bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;

		}

		return bitmap;

	}


}
